package com.it.intelligent_team.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.intelligent_team.common.ErrorCode;
import com.it.intelligent_team.constant.CommonConstant;
import com.it.intelligent_team.exception.BusinessException;
import com.it.intelligent_team.exception.ThrowUtils;
import com.it.intelligent_team.mapper.MeetingMapper;
import com.it.intelligent_team.manager.model.dto.meeting.MeetingQueryRequest;
import com.it.intelligent_team.manager.model.entity.Meeting;
import com.it.intelligent_team.manager.model.vo.MeetingVO;
import com.it.intelligent_team.service.MeetingService;
import com.it.intelligent_team.service.UserService;
import com.it.intelligent_team.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 会议服务实现
 *
 */
@Service
@Slf4j
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param meeting
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validMeeting(Meeting meeting, boolean add) {
        ThrowUtils.throwIf(meeting == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值

        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则

        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则

    }

    /**
     * 获取查询条件
     *
     * @param meetingQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Meeting> getQueryWrapper(MeetingQueryRequest meetingQueryRequest) {
        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();
        if (meetingQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = meetingQueryRequest.getId();
        String sortField = meetingQueryRequest.getSortField();
        String sortOrder = meetingQueryRequest.getSortOrder();
        String searchText = meetingQueryRequest.getSearchText();

        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            // queryWrapper.and(qw -> qw.like("", searchText).or().like("", searchText));
        }
        // 模糊查询

        // JSON 数组查询

        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);

        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取会议封装
     *
     * @param meeting
     * @param request
     * @return
     */
    @Override
    public MeetingVO getMeetingVO(Meeting meeting, HttpServletRequest request) {
        // 对象转封装类
        MeetingVO meetingVO = MeetingVO.objToVo(meeting);

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除


        return meetingVO;
    }


    /**
     * 分页获取会议封装
     *
     * @param meetingPage
     * @param request
     * @return
     */
    @Override
    public Page<MeetingVO> getMeetingVOPage(Page<Meeting> meetingPage, HttpServletRequest request) {
        List<Meeting> meetingList = meetingPage.getRecords();
        Page<MeetingVO> meetingVOPage = new Page<>(meetingPage.getCurrent(), meetingPage.getSize(), meetingPage.getTotal());
        if (CollUtil.isEmpty(meetingList)) {
            return meetingVOPage;
        }
        // 对象列表 => 封装对象列表
        List<MeetingVO> meetingVOList = meetingList.stream().map(meeting -> {
            return MeetingVO.objToVo(meeting);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除


        meetingVOPage.setRecords(meetingVOList);
        return meetingVOPage;
    }


    /**
    * 批量插入数据
    *
    * @param meetingList 数据列表
    * @return 是否成功
    */
    @Override
    public boolean saveBatch(List<Meeting> meetingList) {
        if (CollUtil.isEmpty(meetingList)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "批量插入数据不能为空");
        }
        // 校验每条数据
        for (Meeting meeting : meetingList) {
            validMeeting(meeting, true);
        }
        return super.saveBatch(meetingList);
    }

    /**
    * 批量查询数据
    *
    * @param ids ID数组
    * @return 数据列表
    */
    @Override
    public List<Meeting> listByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "批量查询ID不能为空");
        }
        return super.listByIds(Arrays.asList(ids));
        }

    /**
    * 批量删除数据
    *
    * @param ids ID数组
    * @return 是否成功
    */
    @Override
    public boolean removeByIds(Long[] ids) {
    if (ids == null || ids.length == 0) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR, "批量删除ID不能为空");
    }
        return super.removeByIds(Arrays.asList(ids));
    }
}
